package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.learning.*;
import com.mathlearning.auth.service.LearningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/learning")
@CrossOrigin(origins = "*")
public class LearningController {

    @Autowired
    private LearningService learningService;

    /**
     * 获取学习分类列表
     * GET /api/learning/categories
     */
    @GetMapping("/categories")
    public ApiResponse<CategoryListResponse> getCategories() {
        try {
            CategoryListResponse response = learningService.getCategories();
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取分类失败: " + e.getMessage());
        }
    }

    /**
     * 获取课程列表
     * GET /api/learning/courses?categoryId=1&difficulty=beginner&page=1&pageSize=10
     */
    @GetMapping("/courses")
    public ApiResponse<CourseListResponse> getCourses(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String difficulty,
            @RequestParam(required = false) String sortBy,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            
            CourseQueryRequest queryRequest = CourseQueryRequest.builder()
                    .categoryId(categoryId)
                    .difficulty(difficulty)
                    .sortBy(sortBy)
                    .page(page)
                    .pageSize(pageSize)
                    .build();
            
            CourseListResponse response = learningService.getCourseList(queryRequest, userId);
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取课程失败: " + e.getMessage());
        }
    }

    /**
     * 获取课程详情
     * GET /api/learning/courses/{courseId}
     */
    @GetMapping("/courses/{courseId}")
    public ApiResponse<CourseDetailResponse> getCourseDetail(
            @PathVariable Long courseId,
            HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            CourseDetailResponse response = learningService.getCourseDetail(courseId, userId);
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取课程详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取视频详情
     * GET /api/learning/videos/{videoId}
     */
    @GetMapping("/videos/{videoId}")
    public ApiResponse<VideoDetailResponse> getVideoDetail(
            @PathVariable Long videoId,
            HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            VideoDetailResponse response = learningService.getVideoDetail(videoId, userId);
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取视频详情失败: " + e.getMessage());
        }
    }

    /**
     * 记录学习进度
     * POST /api/learning/progress
     */
    @PostMapping("/progress")
    public ApiResponse<RecordProgressResponse> recordProgress(
            @RequestBody RecordProgressRequest request,
            HttpServletRequest httpRequest) {
        try {
            Long userId = (Long) httpRequest.getAttribute("userId");
            RecordProgressResponse response = learningService.recordProgress(request, userId);
            return ApiResponse.success("记录成功", response);
        } catch (Exception e) {
            return ApiResponse.error("记录进度失败: " + e.getMessage());
        }
    }

    /**
     * 获取课程进度
     * GET /api/learning/courses/{courseId}/progress
     */
    @GetMapping("/courses/{courseId}/progress")
    public ApiResponse<CourseProgressResponse> getCourseProgress(
            @PathVariable Long courseId,
            HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            CourseProgressResponse response = learningService.getCourseProgress(courseId, userId);
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取课程进度失败: " + e.getMessage());
        }
    }
}
